#include "rules.h"

Rules::Rules(std::string n, int i)
    : nt(n), number_id(i), repeat_itself(false)
{
    rules.push_back("");
}
Rules::Rules()
    : number_id(0), repeat_itself(false)
{
    rules.push_back("");
}
Rules::Rules(int i)
    : number_id(i), repeat_itself(false)
{
    rules.push_back("");
}
int Rules::ruleSymbolCounter(int n) const
{
    int c = 0;
    for (uint i = 0; i < rules[n].size();) {
        if (isspace(rules[n][i]))
        {
            ++i;
            continue;
        }
        if (islower(rules[n][i]))
        {
            while (islower(rules[n][i])) ++i;
            ++c;
        }
        else if (!isalpha(rules[n][i]) && !isdigit(rules[n][i])) // символи {,(,),+...
        {
            if ((rules[n][i] == '>') || (rules[n][i] == '<') || (rules[n][i] == '=')) // забезпечуємо правильну обробку >=, <=, ==
                if (rules[n].size() > (i + 1))
                    if (rules[n][i + 1] == '=') ++i;
            ++i;
            ++c;
        }
        else if (isdigit(rules[n][i]))
        {
            while (isdigit(rules[n][i])) ++i;
            ++c;
        }
        else if (isupper(rules[n][i]))
        {
            while (isupper(rules[n][i]) || isdigit(rules[n][i])) ++i;
            ++c;
        }
    }
    return c;
}
